package com.loooooo.pm2024.modules.pm.service;


import com.loooooo.pm2024.exception.MyBaseException;
import com.loooooo.pm2024.modules.pm.mapper.TaskLogMapper;
import com.loooooo.pm2024.modules.pm.mapper.TaskMapper;
import com.loooooo.pm2024.modules.pm.model.Task;
import com.loooooo.pm2024.modules.pm.model.TaskLog;
import com.loooooo.pm2024.modules.pm.selector.TaskLogSelector;
import com.loooooo.pm2024.modules.pm.selector.TaskSelector;
import com.loooooo.pm2024.security.SecurityUtils;
import com.loooooo.pm2024.service.IdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class TaskLogService {

    @Autowired
    TaskLogMapper taskLogMapper;
    @Autowired
    IdService idService;

    public TaskLog get(Long id) {
        return taskLogMapper.get(id);
    }


    public List<TaskLog> list(TaskLogSelector selector) {
        return taskLogMapper.select(selector);
    }

    public int count(TaskLogSelector selector) {
        return taskLogMapper.count(selector);
    }

    public int update(TaskLog issue) {
        long now = System.currentTimeMillis();
        issue.setUpdatedAt(now);
        Long userId = SecurityUtils.loginedUserId();
        issue.setUpdatedBy(userId);

        if (issue.getId() == null || issue.isNewEntity() || issue.getId() <= 0L) {
            if (userId == null) {
                MyBaseException.throwMe(10401, "未登录");
            }
            if (issue.getId() == null || issue.getId() <= 0L) {
                issue.setId(idService.nextId());
            }
            if (issue.getOwnerId() == null) {
                MyBaseException.throwMe(10401, "用户未指定");
            }

            if (!StringUtils.hasText(issue.getType())) {
                MyBaseException.throwMe(10401, "日志类型未指定。");
            }
//            if (issue.getIssueId() == null) {
//                MyBaseException.throwMe(10401, "Issue未指定");
//            }
//            if (issue.getTaskId() == null) {
//                MyBaseException.throwMe(10401, "Task未指定");
//            }
            issue.setCreatedAt(now);
            issue.setCreatedBy(userId);
            taskLogMapper.insert(issue);
        }
        return taskLogMapper.update(issue);
    }

}
